package com.vietthanh85.android;

import java.util.Date;
import java.util.Random;
import java.util.Map.Entry;

import android.app.Activity;
import android.os.Bundle;
import android.text.ClipboardManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class RandomWord extends Activity {
		
	TextView wordTV;
	TextView typeTV;
	TextView meaningTV;
	TextView clientTV;
	TextView timeTV;
	Button backBtn;
	Button showBtn;
	Button nextBtn;
	Button seqBtn;
	Button randBtn;
	Button forgotBtn;
	Button searchBtn;
	Button addBtn;

	int currentID;
	int id;
	int lineNum;
	Random myRandom = new Random();

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.randomword);

		wordTV = (TextView) findViewById(R.id.wordTV);
		typeTV = (TextView) findViewById(R.id.typeTV);
		meaningTV = (TextView) findViewById(R.id.meaningTV);
		clientTV = (TextView) findViewById(R.id.clientTV);
		timeTV = (TextView) findViewById(R.id.timeTV);

		typeTV.setVisibility(View.INVISIBLE);
		if (!Common.reversed) {
			wordTV.setVisibility(View.VISIBLE);
			meaningTV.setVisibility(View.INVISIBLE);
		} else {
			wordTV.setVisibility(View.INVISIBLE);
			meaningTV.setVisibility(View.VISIBLE);
		}

		backBtn = (Button) findViewById(R.id.Back);
		showBtn = (Button) findViewById(R.id.Show);
		nextBtn = (Button) findViewById(R.id.Next);

		randBtn = (Button) findViewById(R.id.Random);
		forgotBtn = (Button) findViewById(R.id.Add2Forgot);
			
		Bundle extras = getIntent().getExtras();
		if (extras == null) {
			lineNum = Common.lineVector.size();
			id = myRandom.nextInt(lineNum);			
		} else {
			id = extras.getInt("id");
			lineNum = extras.getInt("lineNum");
		}
		randomClicked(id);
		
		backBtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				randomClicked(currentID - 1);
			}
		});

		showBtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				wordTV.setVisibility(View.VISIBLE);
				typeTV.setVisibility(View.VISIBLE);
				meaningTV.setVisibility(View.VISIBLE);
			}
		});

		nextBtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				randomClicked(currentID + 1);
			}
		});
		
		randBtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				id = myRandom.nextInt(lineNum);
				randomClicked(id);
			}
		});
		
		forgotBtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				String liteType = "<later>";
				for (Entry<String, String> entry : Common.typeMap.entrySet()) {
					if (entry.getValue().equals(typeTV.getText().toString())) {
						liteType = entry.getKey();
					}
				}	
				String curentDateandTime = Common.sdf.format(new Date());
				Line lineIns = new Line();	
				lineIns.word = wordTV.getText().toString();
				lineIns.word = lineIns.word.substring(lineIns.word.indexOf(" ") + 1);
				lineIns.type = liteType;
				lineIns.meaning = meaningTV.getText().toString();
				lineIns.client = Common.client;
				lineIns.time = curentDateandTime;
				int loc = Common.word2Loc(Common.forgotLineVector, lineIns.word);
				if (loc == -1) {
					lineIns.id = Common.forgotLineVector.size() + 1;
					Common.forgotLineVector.add(lineIns);
				} else {
					lineIns.id = loc + 1;
					Common.forgotLineVector.remove(loc);
					Common.forgotLineVector.insertElementAt(lineIns, loc);
				}
				Toast.makeText(RandomWord.this, "Added to ForgotList!", Toast.LENGTH_LONG).show();
				Common.forgotListChanged = true;
			}
		});
		
		wordTV.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
				String word = wordTV.getText().toString();
				String copiedText = word.substring(word.indexOf(" ") + 1, word.length());
				Toast.makeText(RandomWord.this,
						"\"" + copiedText + "\" is copied to Clipboard",
						Toast.LENGTH_LONG).show();
				clipboard.setText(copiedText);				
			}
		});
		
		meaningTV.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
				Toast.makeText(RandomWord.this, "\"" + meaningTV.getText() + "\" is copied to Clipboard", Toast.LENGTH_LONG).show();
				clipboard.setText(meaningTV.getText());				
			}
		});
	}

	public void randomClicked(int id) {
		typeTV.setVisibility(View.INVISIBLE);
		if (!Common.reversed) {
			wordTV.setVisibility(View.VISIBLE);
			meaningTV.setVisibility(View.INVISIBLE);
		} else {
			wordTV.setVisibility(View.INVISIBLE);
			meaningTV.setVisibility(View.VISIBLE);
		}
		Line lineIns = Common.lineVector.get(id);
		wordTV.setText(id + 1 + ". " + lineIns.word);
		typeTV.setText(Common.typeMap.get(lineIns.type));		
		if (lineIns.meaning.length() > 27) {
			meaningTV.setTextSize(16);			
		} else {
			meaningTV.setTextSize(24);
		}
		meaningTV.setText(lineIns.meaning);
		clientTV.setText(lineIns.client);
		timeTV.setText(lineIns.time);
		if (id + 1 < lineNum) {
			nextBtn.setVisibility(View.VISIBLE);
		} else {
			nextBtn.setVisibility(View.INVISIBLE);
		}
		if (id > 0) {
			backBtn.setVisibility(View.VISIBLE);
		} else {
			backBtn.setVisibility(View.INVISIBLE);
		}
		currentID = id;
	}
}
